package cn.psvmc.cxzapi.interceptors;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Author WN
 * @Description 跨域（协议、域名、端口、子域名不一致）解决方案
 * 1、返回新的CorsFilter
 * 2、重写WebMvcConfigurer
 * 3、使用注解@CrossOrigin
 * 4、手动设置响应头(HttpServletResponse)
 * 前两种属于全局设置，后两种属于局部设置；如果使用了局部设置会覆盖全局的设置，可以通过@CrossOrigin进行细粒度控制
 * 2.4.0之前用allowedOrigins("*")，之后用allowedOriginPatterns("*")
 * @Date 2019/4/3 15:50
 **/
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Resource
    private RequestInterceptor requestInterceptor;

    /**
     * 拦截器addPathPatterns用于添加拦截规则 excludePathPatterns 用于排除拦截
     * @Param registry
     * @Return void
     * @Author wn
     * @Date 2019/4/8
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor).addPathPatterns("/**");
    }

    /**
     * 允许跨域
     * @Param registry
     * @Return void
     * @Author wn
     * @Date 2019/4/8
     */
    /*@Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }*/
}
